package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.apis.wemedia.IWemediaClient;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.UserRealnameDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserRealnameService;
import com.heima.user.service.ApUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {


    @Autowired
    private ApUserMapper userMapper;

    @Autowired
    private IWemediaClient iWemediaClient;

    /**
     * 分页查询用户认证列表
     * @param dto 数据
     * @return 认证数据表
     */
    @Override
    public ResponseResult listAll(UserRealnameDto dto) {
        //校验参数
        if (null == dto){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //构造分页对象
        IPage<ApUserRealname> page = new Page<>(dto.getPage(),dto.getSize());

        LambdaQueryWrapper<ApUserRealname> wrapper = new LambdaQueryWrapper<>();
        //根据审核状态做条件查询
        wrapper.eq(null != dto.getStatus(),ApUserRealname::getStatus,dto.getStatus());

        this.page(page, wrapper);

        return ResponseResult.okResult(page);
    }

    /**
     * 审核失败
     *
     * @param dto 数据
     * @return 认证数据表
     */
    @Override
    public ResponseResult authFail(UserRealnameDto dto) {
        //校验参数
        if (null == dto){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApUserRealname apUserRealname = this.getById(dto.getId());
        apUserRealname.setStatus(ApUserRealname.Status.AUDIT_FAILED.getCode());
        apUserRealname.setReason(dto.getMsg());
        this.updateById(apUserRealname);

        //跟新用户信息
        ApUser apUser = userMapper.selectById(apUserRealname.getUserId());
        //是否认证
        apUser.setIsIdentityAuthentication(0);
        //锁定
        apUser.setStatus(1);
        userMapper.updateById(apUser);
        return ResponseResult.okResult("成功");
    }

    /**
     * 审核通过
     *
     * @param dto 数据
     * @return 认证数据表
     */
    @Override
    public ResponseResult authPass(UserRealnameDto dto) {
        //校验参数
        if (null == dto){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUserRealname apUserRealname = this.getById(dto.getId());
        apUserRealname.setStatus(ApUserRealname.Status.AUDIT_PASS.getCode());
        //更新
        this.updateById(apUserRealname);

        //跟新用户信息
        ApUser apUser = userMapper.selectById(apUserRealname.getUserId());
        //是否认证
        apUser.setIsIdentityAuthentication(1);
        //锁定
        apUser.setStatus(0);
        userMapper.updateById(apUser);

        ResponseResult responseResult = iWemediaClient.addUser(apUser);
        if (responseResult.getCode() == 200){
            return ResponseResult.okResult("成功");
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }
}
